package com.cmcc.example.face.noSdk.controller;

import com.cmcc.example.face.common.vo.ResultVO;
import com.cmcc.example.face.noSdk.common.vo.response.EcloudBaseResponseVO;
import com.cmcc.example.face.noSdk.service.FaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * <p>
 * <name>FaceController</name>
 * 人脸检测相关接口
 * </p >
 *
 * @author arabira
 * @version 1.0 2020-04-01
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/nosdk/face")
public class FaceController {

    @Autowired
    private FaceService faceService;

    /**
     * 人脸比对
     * @param image1
     * @param image2
     * @return
     */
    @RequestMapping(value = "/matcher", method = RequestMethod.POST)
    public ResultVO faceMatcher(@RequestParam("img1")MultipartFile image1, @RequestParam("img2") MultipartFile image2) {
        log.info("接收到两张对比图片 image1:{} image2:{}", image1.getName(), image2.getName());
        Exception exception = new Exception("");
        try {
            EcloudBaseResponseVO responseVO = faceService.matcher(image1, image2);
            return ResultVO.success(responseVO.getBody());
        } catch (IOException e) {
            log.error("发生异常:{}", e);
            exception = e;
        }
        return ResultVO.serverError(exception.getMessage());
    }

    /**
     * 人脸检测
     * @param image
     * @return
     */
    @RequestMapping(value = "/detector", method = RequestMethod.POST)
    public ResultVO faceDetector(@RequestParam("img") MultipartFile image) {
        log.info("接收到图片，请求进行人脸检测 image:{}", image.getName());
        Exception exception = new Exception("");
        try {
            EcloudBaseResponseVO responseVO = faceService.detector(image);
            return ResultVO.success(responseVO.getBody());
        } catch (IOException e) {
            log.error("发生异常:{}", e);
            exception = e;
        }
        return ResultVO.serverError(exception.getMessage());
    }

    /**
     * 人脸检测并返回绘制的图片
     * @param image
     * @param response
     * @return
     */
    @PostMapping("/detect/image")
    public void faceDetecorWithImage(@RequestParam("img") MultipartFile image, HttpServletResponse response) {
        try {
            log.info("接收到图片，请求对图片进行人脸标记 image:{}", image.getName());
            EcloudBaseResponseVO<File> responseVO = faceService.detectAndDraw(image);
            response.setContentType("image/jpeg");
            byte[] bytes = FileCopyUtils.copyToByteArray(responseVO.getBody());
            response.getOutputStream().write(bytes);
            response.getOutputStream().close();
            responseVO.getBody().delete();
        } catch (IOException e) {
            log.error("{}", e);
        }
    }
}